/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modulo3;

import Modelo.DAL.ConexionBD;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONObject;
import Acceso.Security;

/**
 *
 * @author glpc_2
 */
public class listarSolicitud extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String[] ALLOWED = {"Administrador","Recibir y Atender Solicitudes"};

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        if (!Security.validateSession(request, ALLOWED)) {
            Security.privilegeError(response);
            return mapping.findForward("errorPriv");
        }

        response.setContentType("text/json");
        JSONObject data = new JSONObject();
        data.put("consultarSolicitudes", consultarSolicitud());

        PrintWriter pw = response.getWriter();
        pw.write(data.toString());
        pw.flush();
        pw.close();

        return mapping.findForward(SUCCESS);
    }

    public JSONObject consultarSolicitud() {
        ConexionBD conex = new ConexionBD();
        JSONObject Sol = new JSONObject();
        JSONObject nombre = new JSONObject();
        JSONObject categoria = new JSONObject();
        
        Statement st;
        ResultSet rs;

        try {
            st = conex.session.createStatement();

            rs = st.executeQuery("SELECT nombre FROM  solicitud group  by nombre");
            int i = 0;
            while (rs.next()) {
                nombre.put("" + i++, rs.getString(1));
            }

            rs = st.executeQuery("SELECT categoria FROM  solicitud group  by categoria");
            i = 0;
            while (rs.next()) {
                categoria.put("" + i++, rs.getString(1));
            }

            Sol.put("nombre", nombre);
            Sol.put("categoria", categoria);

        } catch (Exception e) {
        }

        return Sol;
    }
}
